﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaskManager
{
    // 此类用于实现，对当日点餐信息的提示逻辑
    class FoodRemind
    {
        public static string remindDate = "";       // 标识是否进行了点餐信息提示

        // 获取今日的点餐信息
        private static String getTodayFoodInfo()
        {
            String Date = Tool.getDate(DateTime.Now); // 获取日期，如 2005-06-07
            //Date = "2016-10-23";

            string sql = "SELECT * FROM `zt_foodlist` WHERE `uaccount`='" + loginForm.userName + "' AND `date`='" + Date + "'";
            string data = Tool.Execute(sql);          // 从数据库中查询对应的数据信息

            return data;
        }

        // 保存点餐提醒信息的日期
        public static void saveTodayFoodInfo()
        {
            String Date = Tool.getDate(DateTime.Now);                           // 获取日期，如 2005-06-07
            Tool.RegistrySave(@"Scimence\TaskManager\Set", "点餐日期", Date);   // 记录此次点餐提醒日期信息
        }

        private static Timer FoodRemind_Timer = null;

        /// <summary>
        /// 根据Data_Screenshot数据执行，定时截屏逻辑
        /// </summary>
        public static void start()
        {
            try
            {
                String Date = Tool.getDate(DateTime.Now);       // 获取日期，如 2005-06-07
                String Str = Tool.RegistryStrValue(@"Scimence\TaskManager\Set", "点餐日期"); // 获取点餐信息记录
                if (!remindDate.Equals(Date) && !Str.Equals(Date))
                {
                    bool inHeFei = Ip_inHeFei();

                    if (/*Tool.todayIsWeekend() ||*/ !inHeFei || loginForm.settings.FoodRemind_time.Equals("不提醒"))  // 周末,或者不在合肥，则不提示点餐信息
                    {
                        remindDate = Date;
                        FoodRemind.saveTodayFoodInfo();
                        return;
                    }
                    else
                    {
                        Timer timer = new Timer();
                        FoodRemind_Timer = timer;

                        timer.Interval = 1000;
                        timer.Tick += FoodRemind_Tick;
                        timer.Enabled = true;
                    }
                }
            }
            catch { }
        }

        private static bool Ip_inHeFei()
        {
            String extenalIp = Tool.GetExtenalIpAddress();  // 获取外网ip
            return (extenalIp.Equals("218.104.71.178") || extenalIp.Equals("61.190.89.191") || extenalIp.Equals("60.173.241.76"));
        }

        /// <summary>
        /// 定时处理逻辑
        /// </summary>
        private static void FoodRemind_Tick(object sender, EventArgs e)
        {
            //loginForm.settings.FoodRemind_time = "09:55:00";
            if (Tool.TimeAbove(loginForm.settings.FoodRemind_time))
            {
                // 关闭定时执行
                FoodRemind_Timer.Enabled = false;
                FoodRemind_Timer = null; 

                // 显示提示信息
                show_Info();
            }
            else if (Tool.TimeAbove("11:55:00")) FoodRemind_Timer.Interval = 1000 * 2;      // 每2秒执行一次
            else if (Tool.TimeAbove("11:50:00")) FoodRemind_Timer.Interval = 1000 * 10;     // 每10秒执行一次
            else if (Tool.TimeAbove("10:00:00")) FoodRemind_Timer.Interval = 1000 * 60;     // 每分钟执行一次
            else if (Tool.TimeAbove("08:00:00")) FoodRemind_Timer.Interval = 1000 * 60 * 5; // 每5分钟执行一次
        }

        // 显示今日点餐信息提示
        private static void show_Info()
        {

            String Date = Tool.getDate(DateTime.Now);       // 获取日期，如 2005-06-07
            if (remindDate.Equals(Date)) return;           // 今天已提醒过，则不再提醒
            else
            {
                String Str = Tool.RegistryStrValue(@"Scimence\TaskManager\Set", "点餐日期"); // 获取点餐信息记录
                if (Str.Equals(Date)) return;                   // 若当日已进行了提醒，则不再提醒

                // 获取点餐信息
                String webData = getTodayFoodInfo();
                if (!webData.Equals(""))
                {
                    remindDate = Date;

                    List<Data_Food> list = Data_Food.Iteams(webData);
                    Data_Food todayFood = (list == null || list.Count == 0) ? null : list[0];

                    if (todayFood != null)  // 未获取到点餐信息时不提示
                    {
                        String info = todayFood.food.Equals("") ? " 今天未点餐！" : ("今天点了," + todayFood.food);
                        new NoticeForm("", TYPE.Food, info, "NEW").Show();          // 点餐提示信息
                    }
                    else new NoticeForm("", TYPE.Food, "请双击查看！", "NEW").Show();  // 点餐提示信息
                }
            }
        }

    }

}
